<template>
  <div class="cp3">
    <div class="center">
      <div ref="content" class="content" @click="handleClick">
        <p
          v-for="(item, index) of messageList"
          :key="index"
          v-html="item.content"
        ></p>
      </div>
      <div class="ChatCollection">
        <ChatCollection
          @handlerCharacter="handlerCharacter"
          ref="chatCollection"
          v-model="text"
        />
      </div>
      <div class="chat-button">
        <button @click="sendText">发送评论</button>
      </div>
    </div>
    <el-dialog
      :show-close="false"
      :visible.sync="dialogVisible"
      width="500px"
      center
    >
      <div
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
        "
        slot="title"
      >
        <el-button type="text">取消</el-button>
        <p style="font-size: 22px">请选择需要通知的用户</p>
        <el-button @click="select" type="text">选择</el-button>
      </div>
      <div>
        <div
          class="userList-item"
          v-for="(item, index) of userList"
          :key="index"
          @click="selectUser(item)"
        >
          <div class="left-content">
            <span>{{ item.name }}</span>
            <span>{{ item.position }}</span>
          </div>
          <span @click="handleCheckbox"
            ><el-checkbox v-model="item.checked"></el-checkbox
          ></span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ChatCollection from "./chatCollection/ChatCollection.vue";
export default {
  components: { ChatCollection },
  data() {
    return {
      dialogVisible: false,
      userList: [
        {
          id: "1",
          name: "张三",
          position: "CDO",
        },
        {
          id: "2",
          name: "李四",
          position: "区域主管",
        },
        {
          id: "3",
          name: "王五",
          position: "开发经理",
        },
      ],

      text: "",
      messageList: [],
      options: {
        disabled: false,
        placeholder: "请输入",
      },
    };
  },
  methods: {
    select() {
      let currentList = this.userList.filter((item) => item.checked);
      currentList.forEach((item) => {
        this.$refs.chatCollection.checkPerson({
          personId: item.id,
          name: item.name,
        });
      });
      this.dialogVisible = false;
    },
    selectUser(item) {
      this.$set(item, "checked", !item.checked);
    },
    handleCheckbox(e) {
      e.stopPropagation();
    },
    handlerCharacter(character) {
      this.dialogVisible = true;
    },
    handleClick(e) {
      console.log(e.target.dataset);
      console.log(e.target.dataset.type);
    },
    sendText() {
      let result = this.$refs.chatCollection.getModel();
      this.messageList.push(result);
      this.$nextTick(() => {
        let element = this.$refs.content;
        element.scrollTop = element.scrollHeight;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.cp3 {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .center {
    width: 500px;
    height: 600px;
    border: 1px solid #ccc;
    .content {
      width: 100%;
      height: 450px;
      overflow: auto;
      p {
        padding: 5px 10px;
      }
    }
    .ChatCollection {
      height: 100px;
    }
    .chat-button {
      height: 50px;
      text-align: right;
      line-height: 50px;
    }
  }
}

.userList-item {
  width: 100%;
  height: 50px;
  background-color: #fff;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  &:hover {
    background-color: #eee;
  }
  .left-content {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    padding-right: 50px;
    & > span {
      &:last-child {
        font-size: 14px;
        color: #999;
      }
    }
  }
}
</style>